//
//  LoginViewController.swift
//  BPAppCliect
//
//  Created by lichentao on 15/1/14.
//  Copyright (c) 2015年 lichentao. All rights reserved.
//  快捷登录

import Foundation
import UIKit

class LoginViewController : BPRootViewController ,UIAlertViewDelegate
{
    @IBOutlet  var submitBtn : UIButton!        // 提交按钮
    @IBOutlet  var mobileField : UITextField!   // 手机号码
    @IBOutlet  var verCodeField : UITextField!  // 验证码输入框
    @IBOutlet  var getCodeBtn : UIButton!       // 获取验证码

    var cancelBtn : UIButton!                   // 返回按钮
    var myTimer : NSTimer?
    var repeatCount : Int = 60
    var getVerCodeSuccess : Bool = false        // 获取验证码状态
    
    override func viewDidLoad() {
        
       super.viewDidLoad()
       self.view.backgroundColor = UIColor.whiteColor()
        
       cancelBtn = UIButton(frame: CGRectMake(0, 80, 40, 35))
       cancelBtn.setTitle("取消", forState: UIControlState.Normal)
       cancelBtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)

       cancelBtn.addTarget(self, action: "cancel", forControlEvents: UIControlEvents.TouchUpInside)
       self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: cancelBtn)

        let timer  = NSTimer(timeInterval: 1, target: self, selector: "timerRepeat", userInfo: nil, repeats: true)
        myTimer = timer
    }
    
    /*
    * Action
    */
    /*取消*/
    @IBAction func cancel(){
       self.navigationController?.popToRootViewControllerAnimated(true)
    }

    /*用户名登录*/
    @IBAction func commonLoginClicked(){

        self.dismissViewControllerAnimated(true, completion: {()->() in
            
        })

    }
    
    /*新用户注册*/
    @IBAction func registerClicked(){

    }
    
    /*提交*/
    @IBAction func submitClicked(){
        
        self.dismissViewControllerAnimated(true, completion: {()->() in
            
        })

        
        // 判断验证码是否成功
        /*
        SMS_SDK.commitVerifyCode(verCodeField.text, result: {(state:SMS_ResponseState) -> () in
            
            if(state.value == SMS_ResponseStateFail.value){
                let sendMsgAlert1 = UIAlertView(title: "验证码错误", message: "", delegate: self, cancelButtonTitle: nil, otherButtonTitles:"OK")
                sendMsgAlert1.show()
                
            }else{
                // 验证码正确，则提交登陆信息
                let params: NSDictionary = ["Mobile": "15011036823", "VerCode": "8421"] as NSDictionary!
                
                let request: BUBaseRequestManager? = BUBaseRequestManager()
                
                request!.requestPostWithParameterWithBlockDic(PATH_BASE.stringByAppendingString(LOGIN_IN), setParamer: params as [NSObject : AnyObject], success: {(responseDictionary)->() in
                    
                    println(responseDictionary)
                    self.showNoticeMsg("注册成功!")
                    self.dismissViewControllerAnimated(true, completion: {()->() in
                        
                    })
                    }, failure: {(errorMsg)->() in
                        
                        self.showNoticeMsg(errorMsg)
                })
            }
            
        })
        */
    }

    
    // timer 计时器
   internal func timerRepeat(){
       let order =  OrderViewController()
        self.navigationController?.pushViewController(order, animated: true)
   }
    // 获取验证码
    @IBAction func getVerCode(){
        myTimer?.fireDate
<<<<<<< HEAD
        var stringMobile:Int?  = mobileField.text!.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)
=======
        let stringMobile:Int?  = mobileField.text!.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)
>>>>>>> 288d846b1206e88894e02a6ccf2ace272f888b1f
        
        if stringMobile != 11 {
            self.showNoticeMsg("请输入正确手机号码")
            return
        }
        
        let sendMsgAlert = UIAlertView(title: "确认手机号码", message: "我们将发送验证码短信到这个号码:" + mobileField.text!, delegate: self, cancelButtonTitle: "取消", otherButtonTitles: "好")
        sendMsgAlert.show()
    }
    
    // UIAlertViewDlegate
    func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
        if buttonIndex == 1 {
           /*
            SMS_SDK.getVerificationCodeBySMSWithPhone(mobileField.text,zone:"86", result:{(error)->() in
                if ((error) == nil)
                {
                    // 获取验证码成功 
                    self.getVerCodeSuccess = true
                }
                else
                {
                    self.getVerCodeSuccess = false
                    let errMsg : String! = "状态信息：" + error.errorDescription
                    let alert = UIAlertView(title: "发送失败", message: errMsg, delegate: self, cancelButtonTitle: "取消", otherButtonTitles: "确认")
                    alert.show()
                }
            })
*/
        }
    }
    
    
}